n = int(input())
nums = list(range(1, n + 1))
index = 1
while len(nums) > 1:
    i = 0
    while i < len(nums):
        num = nums[i]
        if num % 7 == 0 or '7' in str(num):
            del nums[i]
        else:
            i += 1
    nums = nums[index:] + nums[:index]
    index = 1
print(nums[0])